package com.kgc.community.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.kgc.community.pojo.Role;
import com.kgc.community.pojo.Scheduling;
import com.kgc.community.pojo.SchedulingType;
import com.kgc.community.service.RoleService;
import com.kgc.community.service.SchedulingService;
import com.kgc.community.service.SchedulingTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("scheduling")
@Controller
public class SchedulingController {

    SimpleDateFormat format = new SimpleDateFormat("HH:mm");

    @Autowired
    SchedulingService schedulingService;
    @Autowired
    RoleService roleService;
    @Autowired
    SchedulingTypeService schedulingTypeService;



    //测试
    @RequestMapping("test.html")
    public String test(){
        return "scheduling_list";
    }

    /**
     * 通过控制器显示insert页面
     * @return
     */
    @RequestMapping("showModifyPage.html")
    public String showInsertPage(ModelMap modelMap,Scheduling scheduling){
        List<Role> roles = roleService.searchRoles();
        modelMap.addAttribute("scheduling",scheduling).addAttribute("roles",roles);
        return "scheduling_list";
    }

    /**
     * 根据角色的id查询属于该角色的排班情况
     * @param modelMap
     * @param roleId
     * @return
     */
    @RequestMapping("searchScheduling.html")
    public String searchScheduling(ModelMap modelMap,Integer roleId){
        List<Scheduling> schedulings = schedulingService.searchSchedulingByRole(roleId);
        if (schedulings!=null && schedulings.size()>0){

            List<SchedulingType> schedulingTypeList = schedulingTypeService.search();
            Map<Integer,List<Scheduling>> map = new HashMap<>();

            for (SchedulingType schedulingType : schedulingTypeList) {
                map.put(schedulingType.getId(),new ArrayList<Scheduling>());
            }
            for (Scheduling scheduling : schedulings) {
                for (SchedulingType schedulingType : schedulingTypeList) {
                    if (scheduling.getTypeId()==schedulingType.getId()){
                        map.get(scheduling.getTypeId()).add(scheduling);
                    }
                }
            }
            modelMap.addAttribute("schedulingMap",map);
        }else {
            modelMap.addAttribute("searchMsg","暂无记录~");
        }
        List<Role> roles = roleService.searchRoles();
        modelMap.addAttribute("roles",roles);
        return "scheduling_list";
    }

    /**
     * 添加新的排版记录
     * @param modelMap
     * @param scheduling
     * @return
     */
    @RequestMapping("insertSelective.html")
    public String insertSelective(ModelMap modelMap, Scheduling scheduling){
        Integer res = schedulingService.addSchedulingSelective(scheduling);
        if (res>0){
            modelMap.addAttribute("modifyMsg","添加成功！");
        }else {
            modelMap.addAttribute("modifyMsg","添加失败！");
        }
        return "forward:/searchScheduling.html";
    }

    /**
     * 更新
     * @param modelMap
     * @param scheduling
     * @return
     */
    @RequestMapping("updateSelective.html")
    public String updateSelective(ModelMap modelMap,Scheduling scheduling){
        Integer res = schedulingService.updateSchedulingSelective(scheduling);
        if (res>0){
            modelMap.addAttribute("modifyMsg","修改成功！");
        }else {
            modelMap.addAttribute("modifyMsg","修改失败！");
        }
        return "forward:/searchScheduling.html";
    }

    /**
     * 异步请求删除
     * @param modelMap
     * @param schedulingId
     * @return
     */
    @ResponseBody
    @PostMapping("deleteSelective.html")
    public Map<String,Object> deleteSelective(ModelMap modelMap, Integer schedulingId){
        Integer res = schedulingService.deleteSchedulingById(schedulingId);
        Map<String,Object> map = new HashMap<>();

        if (res>0){
            map.put("status",1);
            map.put("info","删除成功！");
        }else {
            map.put("status",0);
            map.put("info","删除失败！");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping("downLoadFile.action")
    public Map<String,Object> downLoadFile(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();

        Map<Integer,List<Scheduling>> schedulingMap = (Map<Integer, List<Scheduling>>) request.getAttribute("schedulingMap");
        Set<Integer> integers = schedulingMap.keySet();
        if (integers!=null){
            for (Integer integer : integers) {
                System.out.println(integer);
            }
        }

//        if (res>0){
//            map.put("status",1);
//            map.put("info","删除成功！");
//        }else {
//            map.put("status",0);
//            map.put("info","删除失败！");
//        }

        return map;
    }
}
